//前台页面分页技术
var tableObj2 = {};
var pagetotal2 = 0;
var pageSize2 = 8;
var startNubmer2 = 0;
var endNumber2 = 0;
var colMData2 = null;
jQuery.extend({
	splitPage2:function(tableObj2Arg,colMData2Arg){
	 colMData2 = colMData2Arg;
	 var disSplitPage2 = document.getElementById("disSplitPage2");
	 var pageSize2Obj = tableObj2Arg.pageSize2;
	 if(pageSize2Obj != null && pageSize2Obj != undefined){
	    pageSize2 = pageSize2Obj;
	 }
	 findData2Count();
	 tableObj2 = tableObj2Arg;
	 var currentPageInput2 = document.getElementById("currentPageInput2");
	 var tableDivId = tableObj2Arg.gridId;
	 if(currentPageInput2 == null || currentPageInput2 == undefined){
	    $("#" + tableDivId).after("<input id='currentPageInput2' type='hidden' value='1'/>");
	 }
	 var pageDiv2 = document.getElementById("pageDiv2");
	 if(pageDiv2 == null || pageDiv2 == undefined){
	    $("#" + tableDivId).after("<div id='pageDiv2'style='height:25px;' class='pagelist'><ul id='splitPageUl2' style='width: 100%'></ul></div>");
	 }
	 var id = "splitPageUl2";
	 var a = "<a id='beforeA' onclick='$.clickBeforePage2("+pagetotal2+")'>上一页</a>";
	    var currentPage = $("#currentPageInput2").val();// 当前页
	    var disSplitPage2 = parseInt($.trim($("#disSplitPage2").val()));// 呈现分页
	    for(var j = disSplitPage2; j <= pagetotal2; j ++){
	    	// pagetotal2小于或等于10显示
	    	if(j > 0 && j <= 10 + disSplitPage2){
	    		a = a + "<a id=splitPageA2" + j + " onclick='$.clickSplitPage2("+ j +"," + pagetotal2 + ")'>"+ j +"</a>";
	    	}
	    	// pagetotal2大于10显示
	    	if(j > 10 + disSplitPage2){
	    		for(var k = 10 + disSplitPage2; k <= pagetotal2; k ++){
	    			$("#splitPageA2"+k).hide();
	    		}
	    	}
	    }
	    a = a + "<a id='afterA2' onclick='$.clickNextPage2("+pagetotal2+")'>下一页</a>";
	    $("#" + id).html(a);
	    
	    // 隐藏(上一页)
	    if(currentPage == "1"){
	    	$("#beforeA").hide();
	    	$("#splitPageA2" + currentPage).attr("class","current");
	    }else{
	    	$("#beforeA").show();
	    	$("#splitPageA2" + currentPage).attr("class","current");
	    }
	   
	    
	    // 隐藏(下一页)
	    if(currentPage == pagetotal2){
	    	$("#afterA2").hide();
	    	$("#splitPageA2" + currentPage).attr("class","current");
	    }else{
	    	if(pagetotal2 != 0){
	    		$("#afterA2").show();
	    		$("#splitPageA2" + currentPage).attr("class","current");
	    	}else{
	    		$("#afterA2").hide();
	    	}
	    }
	    
	    
	     clickSplitPageNum2();
    },
    
    // 绑定点击分页(num)
    clickSplitPage2:function(num,pagetotal2){
    	 var currentPage = $("#splitPageA2" + num).html();
    	  $("#currentPageInput2").val(currentPage);
    	  
    	  // 点击分页判断要 显示的(num)
    	  if(num -5 > 0 && num + 5 <= pagetotal2){
    		  $("#disSplitPage2").val(num-5);
    	  }
    	  
    	 //点击分页判断要 显示的(num)
    	  if(num -5 > 0 && num + 5 > pagetotal2){
    	      if(pagetotal2 - 10 > 0){
    		    $("#disSplitPage2").val(pagetotal2 - 10);
    	      }else{
    	        $("#disSplitPage2").val(1);
    	      }
    	  }
    	  
    	  if(num-5 <= 0){
    	      $("#disSplitPage2").val(1);
    	  }
    	  clickSplitPageNum2()
    },
    
    // 点上一页
    clickBeforePage2:function(pagetotal2){
    	 var currentPage = $("#currentPageInput2").val();// 获取当前页
    	 var beforeNum = eval(currentPage -1);// 获取上一页的(num)
    	 $("#currentPageInput2").val(beforeNum);// 加入隐藏
    	 // 点击分页判断要 显示的(num)
    	  if(beforeNum -5 > 0 && beforeNum + 5 <= pagetotal2){
    		  $("#disSplitPage2").val(beforeNum-5);
    	  }
    	  
    	 //点击分页判断要 显示的(num)
    	  if(beforeNum -5 > 0 && beforeNum + 5 > pagetotal2){
    		  $("#disSplitPage2").val(pagetotal2 - 10);
    	  }
    	 clickSplitPageNum2();
    },
    
    // 点下一页
    clickNextPage2:function(pagetotal2){
     var currentPage = $("#currentPageInput2").val();// 获取当前页
   	 var nextNum = parseInt(currentPage) + 1;// 获取下一页的(num)
   	 $("#currentPageInput2").val(nextNum);// 加入隐藏
   	  // 点击分页判断要 显示的(num)
   	  if(nextNum -5 > 0 && nextNum + 5 <= pagetotal2){
   		  $("#disSplitPage2").val(nextNum-5);
   	  }
   	  
   	  //点击分页判断要 显示的(num)
   	  if(nextNum -5 > 0 && nextNum + 5 > pagetotal2){
   		  $("#disSplitPage2").val(pagetotal2 - 10);
   	  }
   	  if((+currentPage) < (+pagetotal2)){
   	    //当前页小于或者等于总页
   	    clickSplitPageNum2();
   	  }
   	 
    }
    
});
//点击分页的方法
function clickSplitPageNum2(){
  block();
  var tableDivId = tableObj2.gridId;
  var tableDivObj = $("#" + tableDivId);
  tableDivObj.html("");
  setPageArgs2();
  var currenPageNumber =  $("#currentPageInput2").val();
  currenPageNumber = currenPageNumber == null || currenPageNumber == undefined || currenPageNumber == "" ? "1" : currenPageNumber;
  var datas = findData2();
  var colM = tableObj2.colM;
  var tableTile = "";
  var title = tableObj2.title;
  if(title != null && title != undefined){
    tableTile = title;
  }
   var gridStore = new Ext.data.JsonStore({data: datas,fields:tableObj2.fields});
    var gridPanel = new Ext.grid.GridPanel({
      renderTo:tableObj2.gridId,
      border: false,
      title:tableTile,
      height: tableObj2.height,
      layout: 'fit',
      cm:colM,
      store:gridStore
   });
   
    var id = "splitPageUl2";
	 var a = "<a id='beforeA' onclick='$.clickBeforePage2("+pagetotal2+")'>上一页</a>";
	    var currentPage = $("#currentPageInput2").val();// 当前页
	    var disSplitPage2 = parseInt($.trim($("#disSplitPage2").val()));// 呈现分页
	    for(var j = disSplitPage2; j <= pagetotal2; j ++){
	    	// pagetotal2小于或等于10显示
	    	if(j > 0 && j <= 10 + disSplitPage2){
	    		a = a + "<a id=splitPageA2" + j + " onclick='$.clickSplitPage2("+ j +"," + pagetotal2 + ")'>"+ j +"</a>";
	    	}
	    	// pagetotal2大于10显示
	    	if(j > 10 + disSplitPage2){
	    		for(var k = 10 + disSplitPage2; k <= pagetotal2; k ++){
	    			$("#splitPageA2"+k).hide();
	    		}
	    	}
	    }
	    a = a + "<a id='afterA2' onclick='$.clickNextPage2("+pagetotal2+")'>下一页</a>";
	    $("#" + id).html(a);
	    
	    // 隐藏(上一页)
	    if(currentPage == "1"){
	    	$("#beforeA").hide();
	    	$("#splitPageA2" + currentPage).attr("class","current");
	    }else{
	    	$("#beforeA").show();
	    	$("#splitPageA2" + currentPage).attr("class","current");
	    }
	    
	    // 隐藏(下一页)
	    if(currentPage == pagetotal2){
	    	$("#afterA2").hide();
	    	$("#splitPageA2" + currentPage).attr("class","current");
	    }else{
	    	if(pagetotal2 != 0){
	    		$("#afterA2").show();
	    		$("#splitPageA2" + currentPage).attr("class","current");
	    	}else{
	    		$("#afterA2").hide();
	    	}
	    }
	    unblock();
}


//查找表格数据
function findData2(){
   var returncolMData2 = [];
   if(colMData2 != null && colMData2 != undefined && colMData2.length > 0){
      var length = colMData2.length;
      if((+endNumber2) <= (length-1)){
         for(var i = ((+startNumber) - 1); i <= endNumber2 ;i++ ){
           returncolMData2[returncolMData2.length] = colMData2[i];
         }
      }else{
         for(var i = ((+startNumber) - 1); i < length; i++){
           returncolMData2[returncolMData2.length] = colMData2[i];
         }
      }
   }
   return returncolMData2;
}
//查找记录总数
function findData2Count(){
   var count = 0;
   if(colMData2 == null || colMData2 == undefined){
      count = 0;
   }else{
	  count = colMData2.length;
   }
   count = count == null || count == undefined || count == "" ? "0" : count;
   pagetotal2 = count % pageSize2 == 0 ? wholeDivision2(count,pageSize2) : wholeDivision2(count,pageSize2) + 1; 
   return count;
}
//整除方法
function wholeDivision2(data1,data2){
    var result = (+data1) / (+data2);
    if(result >= 0){
      result = Math.floor(result);
    }else{
      result = Math.ceil(result);
    }
    return result;
}
//设置分页参数
function setPageArgs2(){
  var currenPageNumber =  $("#currentPageInput2").val();
  currenPageNumber = currenPageNumber == null || currenPageNumber == undefined || currenPageNumber == "" ? "1" : currenPageNumber;
  startNumber = ((+currenPageNumber) - 1) * pageSize2 + 1;
  endNumber2 = (+currenPageNumber) * pageSize2;
}
